package org.kabeja.dxf.helpers;

import org.kabeja.dxf.entity.DXFPolyline;
import org.kabeja.dxf.entity.DXFSpline;
import org.kabeja.dxf.entity.DXFVertex;
import org.kabeja.math.NURBS;
import org.kabeja.math.NURBSFixedNTELSPointIterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author shutao
 * @since 2022/4/25 10:13
 */
public class DXFSplineConverter {
    public DXFSplineConverter() {
    }

    public static DXFPolyline toDXFPolyline(DXFSpline var0) {
        DXFPolyline var1 = new DXFPolyline();
        var1.setDXFDocument(var0.getDXFDocument());
        if (var0.getDegree() > 0 && var0.getKnots().length > 0) {
            NURBSFixedNTELSPointIterator var4 = new NURBSFixedNTELSPointIterator(toNurbs(var0), 30);

            while(var4.hasNext()) {
                var1.addVertex(new DXFVertex((Point)var4.next()));
            }
        } else {
            Iterator<SplinePoint> var2 = var0.getSplinePointIterator();

            while(var2.hasNext()) {
                SplinePoint var3 = var2.next();
                if (var3.isControlPoint()) {
                    var1.addVertex(new DXFVertex(var3));
                }
            }
        }

        if (var0.isClosed()) {
            var1.setFlags(1);
        }

        return var1;
    }

    public static NURBS toNurbs(DXFSpline var0) {
        Iterator<SplinePoint> var1 = var0.getSplinePointIterator();
        List<SplinePoint> var2 = new ArrayList<>();

        while(var1.hasNext()) {
            SplinePoint var3 = var1.next();
            if (var3.isControlPoint()) {
                var2.add(var3);
            }
        }

        NURBS var4 = new NURBS(var2.toArray(new Point[0]), var0.getKnots(), var0.getWeights(), var0.getDegree());
        var4.setClosed(var0.isClosed());
        return var4;
    }
}
